package com.stjb.furns.web;

import com.google.gson.Gson;
import com.stjb.furns.entity.Cart;
import com.stjb.furns.entity.CartItem;
import com.stjb.furns.entity.Furn;
import com.stjb.furns.service.FurnsService;
import com.stjb.furns.service.impl.FurnsServiceImpl;
import com.stjb.furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author situ
 * @version 1.0
 * @date 2025/5/29 0:59
 * @Description:
 */
public class CartServlet extends BasicServlet {

    private FurnsService furnsService = new FurnsServiceImpl();

    /**
     * 清空购物车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void clean(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取session中的购物车
        Cart cart = (Cart)req.getSession().getAttribute("cart");
        if (cart!=null){
            cart.clean();
        }
        //返回清空购物车原页面
        resp.sendRedirect(req.getHeader("Referer"));


    }

    /**
     * 更新item数量 更新购物车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        int count = DataUtils.parseInt(req.getParameter("count"),1);

        //获取session中的购物车
        Cart cart = (Cart)req.getSession().getAttribute("cart");
        if (cart!=null){//更新购物车
            cart.updateCount(id,count);
        }
        //重定向到更新购物车页面 防止多次提交
        resp.sendRedirect(req.getHeader("Referer"));



    }



    protected void delItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        // String id1 = req.getParameter("id");
        //获取cart 购物车
        Cart cart = (Cart)req.getSession().getAttribute("cart");
        System.out.println(cart);
        if (cart!=null){
            cart.delItem(id);
        }
        // System.out.println(cart);
        // System.out.println(id1);
        //重定向到请求删除购物车界面
        resp.sendRedirect(req.getHeader("Referer"));
    }

    /**
     * 添加家具信息到购物车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //通过session信息获取购物车商品id
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        //获取id对应的Furns对象
        Furn furn = furnsService.queryFurnById(id);
        //如果为null return
        if (furn==null){
            return;
        }

        //根据furn 构建CartItem
        CartItem item = new CartItem(furn.getId(), furn.getName(), furn.getPrice(), 1, furn.getPrice());

        //通过session信息获取购物车商品cart
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        if (null == cart){//说明当前用户session中没有cart
            cart = new Cart();
            req.getSession().setAttribute("cart",cart);
        }

        //将cartItem加入到cart
        cart.addItem(item);

        //返回添加家居界面
        //根据请求头 得到当前页面url
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);


    }


    protected void addItemByAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //通过前端参数获取购物车商品id
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        //获取id对应的Furns对象
        Furn furn = furnsService.queryFurnById(id);
        //如果为null return
        if (furn==null){
            return;
        }
        //根据furn 构建CartItem
        CartItem item = new CartItem(furn.getId(), furn.getName(), furn.getPrice(), 1, furn.getPrice());

        //通过session信息获取购物车商品cart
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (null == cart){//说明当前用户session中没有cart
            cart = new Cart();
            req.getSession().setAttribute("cart",cart);
        }

        //将cartItem加入到cart
        cart.addItem(item);

        //添加完毕后 返回json数据到前端
        //前端局部刷新
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("cartTotalCount", cart.getTotalCount());
        Gson gson = new Gson();
        String resultGson = gson.toJson(resultMap);
        //发送到前端
        resp.getWriter().write(resultGson);
    }
}
